package com.ten.utils;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.util.DigestUtils;

public class MyDigestUtil {
	public static String stringMD5(String pw){
      try {
    	// 拿到一个MD5转换器（如果想要SHA1参数换成”SHA1”）  
		MessageDigest messageDigest =MessageDigest.getInstance("MD5");
		// 输入的字符串转换成字节数组  
        byte[] inputByteArray = pw.getBytes("UTF-8");
        // inputByteArray是输入字符串转换得到的字节数组  
        messageDigest.update(inputByteArray);
     // 转换并返回结果，也是字节数组，包含16个元素  
        byte[] resultByteArray = messageDigest.digest();  
        // 字符数组转换成字符串返回  
       return byteArrayToHex(resultByteArray);  
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return "";
	}
	}
	public static String byteArrayToHex(byte[] byteArray) {  
        
        // 首先初始化一个字符数组，用来存放每个16进制字符  
        char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' };  
        // new一个字符数组，这个就是用来组成结果字符串的（解释一下：一个byte是八位二进制，也就是2位十六进制字符（2的8次方等于16的2次方））  
        char[] resultCharArray =new char[byteArray.length * 2];  
        // 遍历字节数组，通过位运算（位运算效率高），转换成字符放到字符数组中去  
        int index = 0; 
        for (byte b : byteArray) {  
           resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];  
           resultCharArray[index++] = hexDigits[b& 0xf];  
        }
        // 字符数组组合成字符串返回  
        return new String(resultCharArray);  
    }
	/**
	 * md5加密
	 * @param psw
	 * @return
	 */
	public static String md5Digest(String psw){
		String sign="";
		String appkey = "4b972f3d88a649308a9ff2a7f9ccc2c4";
		try {
			sign = DigestUtils.md5DigestAsHex((psw+appkey).getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sign;
	}
	/**
	 * 日期转化方法 格式 yyyy-MM-dd HH24:mi:ss
	 * @return
	 */
	public static String dateFormate(Date date){
		String str ="";
		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 str =format.format(date);
		return str;
	}
	
	public static String doGet(String url, String header,Map<String, String> querys){
		// 创建Httpclient对象
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			
			// 创建uri
			URIBuilder builder = new URIBuilder(url);
			if (querys != null) {
				for (String key : querys.keySet()) {
					builder.addParameter(key, querys.get(key));
				}
			}
			URI uri = builder.build();
			// 创建http GET请求
			HttpGet httpGet = new HttpGet(uri);
			httpGet.setHeader("Authorization",header);
			// 执行请求
			response = httpclient.execute(httpGet);
			// 判断返回状态是否为200
			if (response.getStatusLine().getStatusCode() == 200) {
					resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
						}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (response != null) {
					response.close();
				}
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}
}


